/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parismetro;

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Stack;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.control.ListView;
import javafx.scene.layout.LayoutInfo;
import modele.RERTicket;
import javafx.scene.CacheHint;
import javafx.scene.control.ListCell;
import javafx.scene.control.Label;

/**
 * @author Manu
 */
// place your code here
public class ChoixStation extends CustomNode {

    var group: Group;
    public var main: Main;
    def back = ImageView {
                image: Image { url: "{__DIR__}icons/back_2-2.png" } }
    public def carte = Map { choixStation: this };
    public def dialog = DialogMap { choixStation: this };
    var titre = "Choisissez votre station d'arrivée.";
    public var departureStation = main.stationMachine;
    
    public var departureSelect = false on replace {
        if (departureSelect) {
            titre = "Choisissez votre station de départ.";
        } else {
            titre = "Choisissez votre station d'arrivée.";
        }
        stationEnCours = "";
    };
    
    def keyboard = Rectangle {
                x: 22, y: 418
                width: 980, height: 350
                arcWidth: 20, arcHeight: 20
                fill: Color.rgb(0, 113, 188)
                opacity: 0.2
            }
    def xligne1 = 50;
    def yligne1 = 430;
    def xligne2 = 80;
    def xligne3 = 60;
    def intercase = 90;
    def a = Stack {
                layoutX: xligne1, layoutY: yligne1
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "A"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}A";
                }
            }
    def z = Stack {
                layoutX: xligne1 + 1 * intercase, layoutY: yligne1
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "Z"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}Z";
                }
            }
    def e = Stack {
                layoutX: xligne1 + 2 * intercase, layoutY: yligne1
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "E"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}E";
                }
            }
    def r = Stack {
                layoutX: xligne1 + 3 * intercase, layoutY: yligne1
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "R"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}R";
                }
            }
    def t = Stack {
                layoutX: xligne1 + 4 * intercase, layoutY: yligne1
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "T"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}T";
                }
            }
    def y = Stack {
                layoutX: xligne1 + 5 * intercase, layoutY: yligne1
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "Y"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}Y";
                }
            }
    def u = Stack {
                layoutX: xligne1 + 6 * intercase, layoutY: yligne1
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "U"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}U";
                }
            }
    def i = Stack {
                layoutX: xligne1 + 7 * intercase, layoutY: yligne1
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "I"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}I";
                }
            }
    def o = Stack {
                layoutX: xligne1 + 8 * intercase, layoutY: yligne1
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "O"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}O";
                }
            }
    def p = Stack {
                layoutX: xligne1 + 9 * intercase, layoutY: yligne1
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "P"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}P";
                }
            }
    def q = Stack {
                layoutX: xligne2, layoutY: yligne1 + 1 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "Q"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}Q";
                }
            }
    def s = Stack {
                layoutX: xligne2 + 1 * intercase, layoutY: yligne1 + 1 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "S"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}S";
                }
            }
    def d = Stack {
                layoutX: xligne2 + 2 * intercase, layoutY: yligne1 + 1 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "D"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}D";
                }
            }
    def f = Stack {
                layoutX: xligne2 + 3 * intercase, layoutY: yligne1 + 1 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "F"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}F";
                }
            }
    def g = Stack {
                layoutX: xligne2 + 4 * intercase, layoutY: yligne1 + 1 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "G"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}G";
                }
            }
    def h = Stack {
                layoutX: xligne2 + 5 * intercase, layoutY: yligne1 + 1 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "H"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}H";
                }
            }
    def j = Stack {
                layoutX: xligne2 + 6 * intercase, layoutY: yligne1 + 1 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "J"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}J";
                }
            }
    def k = Stack {
                layoutX: xligne2 + 7 * intercase, layoutY: yligne1 + 1 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "K"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}K";
                }
            }
    def l = Stack {
                layoutX: xligne2 + 8 * intercase, layoutY: yligne1 + 1 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "L"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}L";
                }
            }
    def m = Stack {
                layoutX: xligne2 + 9 * intercase, layoutY: yligne1 + 1 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "M"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}M";
                }
            }
    def w = Stack {
                layoutX: xligne3, layoutY: yligne1 + 2 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "W"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}W";
                }
            }
    def x = Stack {
                layoutX: xligne3 + 1 * intercase, layoutY: yligne1 + 2 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "X"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}X";
                }
            }
    def c = Stack {
                layoutX: xligne3 + 2 * intercase, layoutY: yligne1 + 2 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "C"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}C";
                }
            }
    def v = Stack {
                layoutX: xligne3 + 3 * intercase, layoutY: yligne1 + 2 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "V"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}V";
                }
            }
    def b = Stack {
                layoutX: xligne3 + 4 * intercase, layoutY: yligne1 + 2 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "B"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}B";
                }
            }
    def n = Stack {
                layoutX: xligne3 + 5 * intercase, layoutY: yligne1 + 2 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_lettre.png" }
                    }
                    Text {
                        font: Font { size: 36 }
                        content: "N"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours}N";
                }
            }
    def space = Stack {
                layoutX: xligne3 + 6 * intercase + 10, layoutY: yligne1 + 2 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_keyboard.png" }
                    }
                    Text {
                        font: Font { size: 30 }
                        content: "Espace"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    stationEnCours = "{stationEnCours} ";
                }
            }
    def corr = Stack {
                layoutX: xligne3 + 8 * intercase, layoutY: yligne1 + 2 * intercase
                width: 980, height: 350
                disable: bind carte.isDialogDiplayed
                content: [
                    ImageView {
                        image: Image { url: "{__DIR__}icons/cadre_keyboard.png" }
                    }
                    Text {
                        font: Font { size: 30 }
                        content: "Correction"
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    if (stationEnCours.length() > 0)
                        stationEnCours = stationEnCours.substring(0, stationEnCours.length() - 1)
                }
            }
    def bouton_autre_depart = Group {
                layoutX: 15;
                layoutY: 70;
                visible: bind not departureSelect;
                disable: bind carte.isDialogDiplayed;
                content: [
                    Rectangle {
                        height: 105
                        width: 210
                        arcWidth: 15, arcHeight: 15
                        fill: Color.rgb(0, 113, 188)
                        stroke: Color.WHITE;
                        strokeWidth: 5;
                    }
                    Text {
                        layoutX: 15, layoutY: 40
                        font: Font { size: 32, embolden: true, name: "Myriad Pro" }
                        content: "Partir d'une\nautre station."
                        textAlignment: TextAlignment.CENTER
                        fill: Color.WHITE
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    main.logger.addEvent("rer", "btnAutreDepart", "rerDepart");
                    departureSelect = true;
                }
            }
    def bouton_etape_prec = Group {
                layoutX: 125;
                layoutY: 190;
                disable: bind carte.isDialogDiplayed;
                content: [
                    Rectangle {
                        height: 95
                        width: 103
                        arcWidth: 15, arcHeight: 15
                        fill: Color.rgb(0, 113, 188)
                        stroke: Color.WHITE;
                        strokeWidth: 5;
                    }
                    Text {
                        layoutX: 1, layoutY: 40
                        font: Font { size: 19, embolden: true, name: "Myriad Pro" }
                        content: "Etape\nPrécédente"
                        textAlignment: TextAlignment.CENTER
                        fill: Color.WHITE
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    if (departureSelect) {
                        main.logger.addEvent("rerDepart", "btnEtapePrec", "rer");
                        departureSelect = false;
                    } else {
                        main.logger.addEvent("rer", "btnEtapePrec", main.optionEcran.previousStep);
                        stationEnCours = "";
                        departureStation = main.stationMachine;
                        main.changeContent(main.optionEcran.previousStep);
                    }
                }
            }
    def bouton_annuler_tout = Group {
                layoutX: 15;
                layoutY: 190;
                disable: bind carte.isDialogDiplayed;
                content: [
                    Rectangle {
                        height: 95
                        width: 100
                        arcWidth: 15, arcHeight: 15
                        fill: Color.rgb(255, 115, 89)
                        stroke: Color.WHITE;
                        strokeWidth: 5;
                    }
                    Text {
                        layoutX: 10, layoutY: 40
                        font: Font { size: 22, embolden: true, name: "Myriad Pro" }
                        content: "Annuler\nTout"
                        textAlignment: TextAlignment.CENTER
                        fill: Color.WHITE
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    main.logger.addEvent(if (departureSelect) "rerDepart" else "rer", "btnAnnulerTout", "accueil");
                    main.orderList.removeAll();
                    stationEnCours = "";
                    departureStation = main.stationMachine;
                    departureSelect = false;
                    main.changeContent("accueil");
                }
            }
    def bouton_map = Group {
                layoutX: 15;
                layoutY: 300;
                disable: bind carte.isDialogDiplayed;
                content: [
                    Rectangle {
                        height: 105
                        width: 210
                        arcWidth: 15, arcHeight: 15
                        fill: Color.rgb(0, 113, 188)
                        stroke: Color.WHITE;
                        strokeWidth: 5;
                    }
                    Text {
                        layoutX: 15, layoutY: 40
                        font: Font { size: 32, embolden: true, name: "Myriad Pro" }
                        content: "Sélectionner\nsur la carte"
                        textAlignment: TextAlignment.CENTER
                        fill: Color.WHITE
                    }
                ]
                onMousePressed: function(e: MouseEvent): Void {
                    main.logger.addEvent(if (departureSelect) "rerDepart" else "rer", "btnMap", "dialogMap");
                    dialog.afficher = true;
                }
            }
    public var stationEnCours = "" on replace { updateShownStations(stationEnCours); };
    def cadre_text = CadreNomStation {
                text: bind stationEnCours;
            }
    def texte_indications = Text {
                layoutX: 400, layoutY: 100
                font: Font { size: 36, name: "Myriad Pro" }
                content: bind titre;
            }
    def station_depart = Group {
                layoutX: 15, layoutY: 50
                visible: bind not departureSelect;
                content: [
                    Rectangle {
                        y: -30
                        fill: Color.WHITE
                        width: 400, height: 40,
                        opacity: 0.6
                    }

                    Text {
                        font: Font { size: 24, name: "Myriad Pro" }
                        content: bind "Station de départ: {departureStation}"
                    }
                ]
            }
    def fileDataSource: org.netbeans.javafx.datasrc.HttpDataSource = org.netbeans.javafx.datasrc.HttpDataSource {
        url: "http://kraynel.free.fr/positionsStations.xml"
        parser: org.netbeans.javafx.datasrc.Parsers.XML_PARSER
    }
    
    var shownStations: String[];

    function updateShownStations(debutNom: String): Void {

        var name: String;
        var records = fileDataSource.getDataSource("Worksheet/Table/Row/Cell/Data").getRecordSet().all();
        var i: Integer = 0;

        shownStations = [];
        var xMap: Integer;
        var yMap: Integer;

        if (debutNom.length() != 0)
            while (i < records.size()) {
                name = records.get(i).getString(".text");
                xMap = java.lang.Integer.parseInt(records.get(++i).getString(".text"));
                yMap = java.lang.Integer.parseInt(records.get(++i).getString(".text"));
                ++i;
                if (name.toLowerCase().indexOf(debutNom.trim().toLowerCase() ) > -1 and not name.toLowerCase().equals(departureStation.toLowerCase())) {
                    insert name into shownStations;
                }
            }
    }

var selectedCell: Integer = -1;

function currentCellFactory():ListCell {
    def cell:ListCell = ListCell {
        node: Label {
        text: bind if (cell.empty) then "" else "{cell.item}"
        onMouseEntered: function(e:MouseEvent) : Void {
            selectedCell = cell.index;
        }
        onMouseExited: function(e:MouseEvent) : Void {
            selectedCell = -1;
        }

        }
    }
}

    def listView: javafx.scene.control.ListView = javafx.scene.control.ListView {
                layoutX: 500;
                layoutY: 180;
                layoutInfo: LayoutInfo { width: 300 height: 100 };
                disable: bind carte.isDialogDiplayed;
                items: bind for (record in shownStations) record
                //style: "font: 35pt Broadway"
                scaleX: 2;
                scaleY: 2;
                cellFactory: currentCellFactory;
                onMouseMoved: function(e:MouseEvent) : Void {
                 listView.select(selectedCell);
                }
                onMouseClicked: function(e: MouseEvent): Void {
                    if (departureSelect) {
                        departureStation = (listView.selectedItem).toString();
                        departureSelect = false;
                    } else {
                        if(listView.selectedItem != null){
                        var t:RERTicket = RERTicket{orderList:main.orderList, startStation: departureStation, endStation:{listView.selectedItem.toString()} };
                        main.orderList.addOrder(t);
                        //println("Achat de billet vers {listView.selectedItem.toString()}");
                        stationEnCours = "";
                        departureStation = main.stationMachine;
                        departureSelect = false;
                        
                        main.optionEcran.previousStep = "rer";
                        main.changeContent("tplus");
                        }
                    }
                }


    }
    //items: bind for(record in fileDataSource.getDataSource("Worksheet/Table/Row/Cell").getRecordSet().all()) record.getString(".text")



override public function create(): Node {
    def defMain: Main = main;
    group = Group { content: [
                    back, keyboard, a, z, e, r, t, y, u, i, o, p, q, s, d, f, g, h, j, k, l, m,
                    w, x, c, v, b, n, space, corr,
                    bouton_etape_prec, bouton_annuler_tout, bouton_autre_depart,
                    cadre_text,
                    texte_indications, station_depart, bouton_map, listView, dialog,
                ]
                    cache:true;
                    cacheHint: CacheHint.SPEED;
            }
    updateShownStations("");
    return group;

}

}